package com.canaan.business.schedule;

import cn.hutool.core.lang.Pair;
import cn.hutool.json.JSONUtil;
import com.canaan.business.domain.vo.GoodInfoVo;
import com.canaan.business.domain.vo.StockAdjustVo;
import com.canaan.business.domain.vo.StorehouseStockAdjustDetailVo;
import com.canaan.business.service.storehouse.IWmsStorehouseStockService;
import com.canaan.common.encrypt.PushEdiService;
import com.canaan.common.utils.DateUtils;
import com.canaan.system.service.ISysConfigService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component("StorehouseStockAdjustSchedule")
public class StorehouseStockAdjustSchedule {
    /**
     * 每页数量
     */
    private final int PAGE_COUNT=50;
    @Resource
    private IWmsStorehouseStockService wmsStorehouseStockService;
    @Resource
    private ISysConfigService sysConfig;


    /**
     * 库存校准
     */
    @ApiOperation(value = "库存校准", notes = "库存校准")
    public void adjust() {
        List<StorehouseStockAdjustDetailVo> details=wmsStorehouseStockService.selectWmsStorehouseStockAdjustList();
        Map<StockAdjustVo,List<GoodInfoVo>> map=new HashMap<>();
        Map<String,StockAdjustVo> stockAdjustVoMap=new HashMap<>();
        int total=0;
        List<String> keys=new ArrayList<>();
        for (StorehouseStockAdjustDetailVo vo :details){
            String key=vo.getOwner()+"_"+vo.getStockCode()+vo.getStockStatus();
            if(!keys.contains(key)){
                keys.add(key);
                StockAdjustVo stockAdjustVo=new StockAdjustVo();
                stockAdjustVo.setStockCode(vo.getStockCode());
                stockAdjustVo.setOwner(vo.getOwner());
                stockAdjustVo.setStockStatus(vo.getStockStatus());
                stockAdjustVoMap.put(key,stockAdjustVo);
                List<GoodInfoVo> goodInfoVos=new ArrayList<>();
                map.put(stockAdjustVo,goodInfoVos);
                GoodInfoVo goodInfoVo=new GoodInfoVo();
                goodInfoVo.setSpecCode(vo.getProductNo());
                goodInfoVo.setStockNum(vo.getQty().toString());
                goodInfoVo.setBatchNo(vo.getBatchNo());
                goodInfoVos.add(goodInfoVo);
            }else{
                if(!stockAdjustVoMap.containsKey(key)){
                    continue;
                }
                if(!map.containsKey(stockAdjustVoMap.get(key))){
                    continue;
                }
                GoodInfoVo goodInfoVo=new GoodInfoVo();
                goodInfoVo.setSpecCode(vo.getProductNo());
                goodInfoVo.setStockNum(vo.getQty().toString());
                goodInfoVo.setBatchNo(vo.getBatchNo());
                map.get(stockAdjustVoMap.get(key)).add(goodInfoVo);
            }
            total++;
        }
        //总页数
        int pages=total/PAGE_COUNT+1;
        int pageCount=0;
        int current=1;
        for(StockAdjustVo item: map.keySet()){
            List<GoodInfoVo> goodInfoVos=map.get(item);
            if(pageCount+goodInfoVos.size()<current*PAGE_COUNT){
                Pair<Boolean, String> result= postStockAdjust(item,pages,current,goodInfoVos);
               if(!result.getKey()){
                   throw new RuntimeException(result.getValue());
               }
               pageCount+=goodInfoVos.size();
            }else if(pageCount+goodInfoVos.size()==current*PAGE_COUNT){
                Pair<Boolean, String> result= postStockAdjust(item,pages,current,goodInfoVos);
                if(!result.getKey()){
                    throw new RuntimeException(result.getValue());
                }
                current++;
                pageCount+=goodInfoVos.size();
            }else{
                int preCount=current*PAGE_COUNT-pageCount;
                List<GoodInfoVo> preInfoVos=goodInfoVos.subList(0,preCount);
                List<GoodInfoVo> lastInfoVos=goodInfoVos.subList(preCount,goodInfoVos.size());
                Pair<Boolean, String> result= postStockAdjust(item,pages,current,preInfoVos);
                if(!result.getKey()){
                    throw new RuntimeException(result.getValue());
                }
                current++;
                result= postStockAdjust(item,pages,current,lastInfoVos);
                if(!result.getKey()){
                    throw new RuntimeException(result.getValue());
                }
                pageCount+=goodInfoVos.size();
            }
        }

    }

    /**
     * 同步库存
     */
    private Pair<Boolean, String> postStockAdjust(StockAdjustVo vo,int total,int currentPage,List<GoodInfoVo> details){
        StockAdjustVo stockAdjustVo=new StockAdjustVo();
        stockAdjustVo.setStockCode(vo.getStockCode());
        stockAdjustVo.setOwner(vo.getOwner());
        stockAdjustVo.setStockStatus(vo.getStockStatus());
        stockAdjustVo.setCompareDate(DateUtils.getDate());
        stockAdjustVo.setCompareBatchNo(DateUtils.getDate());
        stockAdjustVo.setItems(details);
        stockAdjustVo.setTotalPage(total);
        stockAdjustVo.setCurrentPage(currentPage);
        Pair<Boolean, String> result = PushEdiService.push(sysConfig.selectConfigByKey("edi.compare"), JSONUtil.toJsonStr(stockAdjustVo));
        return result;
    }
}
